package com.scm.dao;

import java.sql.*;
import java.util.*;

import com.scm.util.DbUtil;

public abstract class BaseDao<T> {
	
	protected Connection conn;
	
	public BaseDao(Connection conn) {
		super();
		this.conn = conn;
	}

	public BaseDao() {
		super();
	}
	
	/**
	 * 分页查询需要实现的查询数据方法
	 * @param queryCondition
	 * @return
	 * @throws SQLException
	 */
	public abstract List<T> selectData(Map<String,String> querymap) throws SQLException;
	
	/**
	 * 分页查询，查询总条数
	 * @param queryCondition
	 * @return
	 * @throws SQLException
	 */
	public int selectcount(Map<String,String> querymap) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			StringBuilder sql = new StringBuilder("select count(*)  ");
			
			Map<Integer, String> params = new HashMap<Integer, String>();
			generateQueryCondition(querymap, params, sql);
			
			ps = conn.prepareStatement(sql.toString());
			
			//给？赋值ֵ
			for(Integer i:params.keySet()){
				ps.setObject(i, params.get(i));
			} 
			rs = ps.executeQuery();
			while(rs.next()){
				return rs.getInt(1);
			}
			return -1;
		}finally{
			DbUtil.close(null, rs, ps);
		}
	}
	
	/**
	 * 生成查询条件的方法
	 * @param queryCondition
	 * @return
	 */
	public abstract int generateQueryCondition(Map<String, String> queryMap,final Map<Integer,String> params,final StringBuilder sql);
}
